home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / dist / gdb-lines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  7.4 KB  |  242 lines

  1. /* gdb-lines.c -- Deal with source lines for GDB format
  2.    Copyright (C) 1989, Free Software Foundation.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.    
  20. #include "as.h"
  21. #include "obstack.h"
  22. #include "frags.h"
  23.  
  24. /* This is a souce file that we're storing .gdbline information about */
  25. /* .gdbline refers to files by numbers.  We keep a linked list of them
  26.    We store a list of vectors for each file.  Each element of the vector
  27.    contains a line-number, a frag, and an offset within the frag. */
  28. struct g_line_file {
  29.     int    gdb_line_file_file_number;        /* fnum */
  30.     int    gdb_line_file_number_of_vectors;    /* nv */
  31.     long    gdb_line_table_offset;            /* taboff */
  32.     struct g_line_vector *gdb_line_file_vectors;    /* vec */
  33.     struct g_line_file *gdb_line_file_next_file;    /* nfile */
  34. };
  35.  
  36. /* In order to save on space (We expect there to be LOTS of lines), we
  37.    store line-number/address pairs in bunches of MAX_LINES_PER_VECTOR
  38.    (originally fifty).  Each vector descriptor contains
  39.  
  40.    gdb_line_number_of_lines    the number of line-number/address pairs
  41.                    actually in this vector.
  42.    gdb_line_lines        The actual vector.
  43.  
  44.    gdb_line_next_vector        The next vector descriptor in the linked list.
  45.  */
  46. struct g_line_vector {
  47.     int    gdb_line_number_of_lines;        /* nlines */
  48.     struct g_line *gdb_line_lines;            /* lines */
  49.     struct g_line_vector *gdb_line_next_vector;    /* nvec */
  50. };
  51.  
  52.  
  53. /* A .gdbline wants to store a line-number/address pair.  Unfortunatly, we
  54.    don't know addresses yet, so we store frag/offset which we can use to
  55.    generate the address at write-out time. */
  56. struct g_line {
  57.     int    gdb_line_line_number;            /* lno */
  58.     fragS    *gdb_line_frag;                /* lfrag */
  59.     int    gdb_line_offset;            /* loff */
  60. };
  61.  
  62.  
  63. /* The following is stolen from (gdb's? (or is it gcc's?) symseg.h file.
  64.    These structures describe the format for the line# symbolic info in
  65.    the gdb symbolic info file.  This info is not particularly useful,
  66.    except to show what we're writing into. . . */
  67.  
  68. /* Source-file information.
  69.    This describes the relation between source files and line numbers
  70.    and addresses in the program text.  */
  71.  
  72. struct sourcevector
  73. {
  74.   int length;            /* Number of source files described */
  75.   struct source *source[1];    /* Descriptions of the files */
  76. };
  77.  
  78. /* Line number and address of one line.  */
  79.  
  80. struct line
  81. {
  82.   int linenum;
  83.   int address;
  84. };
  85.  
  86. /* All the information on one source file.  */
  87.  
  88. struct source
  89. {
  90.   char *name;            /* Name of file */
  91.   int nlines;            /* Number of lines that follow */
  92.   struct line lines[1];    /* Information on each line */
  93. };
  94.  
  95. /* End of text from symseg.h */
  96.  
  97. struct g_line_file *first_file;
  98.  
  99. struct g_line_file *add_file();
  100. struct g_line_vector *add_vector();
  101.  
  102. #define MAX_LINES_PER_VECTOR 50        /* lpv */
  103.  
  104. /* We've been told that the current address corresponds to line LINENO in
  105.    file FILE_NUMBER */
  106. void
  107. gdb_line(file_number,lineno)
  108. int file_number;
  109. int lineno;
  110. {
  111.     struct g_line_file *f;
  112.     struct g_line_vector *v;
  113.     struct g_line *line;
  114.  
  115.     for(f=first_file;f;f=f->gdb_line_file_next_file)
  116.         if(f->gdb_line_file_file_number==file_number)
  117.             break;
  118.     if(!f) f=add_file(file_number);
  119.     v=f->gdb_line_file_vectors;
  120.     if(!v || v->gdb_line_number_of_lines==MAX_LINES_PER_VECTOR)
  121.         v=add_vector(f);
  122.     line= &(v->gdb_line_lines)[v->gdb_line_number_of_lines];
  123.     v->gdb_line_number_of_lines++;
  124.     line->gdb_line_line_number=lineno;
  125.     line->gdb_line_frag= frag_now;
  126.     line->gdb_line_offset=obstack_next_free(&frags)-frag_now->fr_literal;
  127. }
  128.  
  129. /* We've been told where to store the .line table for file FILE_NUMBER */
  130. void
  131. gdb_line_tab(file_number,offset)
  132. int file_number;
  133. int offset;
  134. {
  135.     struct g_line_file *f;
  136.  
  137.     for(f=first_file;f;f=f->gdb_line_file_next_file)
  138.         if(f->gdb_line_file_file_number==file_number)
  139.             break;
  140.     if(!f) f=add_file(file_number);
  141.     if(f->gdb_line_table_offset)
  142.         as_warn("Ignoring duplicate .linetab for file %d",file_number);
  143.     else
  144.         f->gdb_line_table_offset=offset;
  145. }
  146.  
  147. /* We've got a file (FILE_NUMBER) that we haven't heard about before.  Create
  148.    an entry for it, etc. . . */
  149. struct g_line_file *
  150. add_file(file_number)
  151. {
  152.     struct g_line_file *f;
  153.  
  154.     f=(struct g_line_file *)xmalloc(sizeof(struct g_line_file));
  155.     f->gdb_line_file_file_number=file_number;
  156.     f->gdb_line_table_offset = 0;
  157.     f->gdb_line_file_number_of_vectors=0;
  158.     f->gdb_line_file_vectors=(struct g_line_vector *)0;
  159.     f->gdb_line_file_next_file=first_file;
  160.     first_file=f;
  161.     return f;
  162. }
  163.  
  164. /* The last vector for file F is full.  Allocate a new one. */
  165. struct g_line_vector *
  166. add_vector(f)
  167. struct g_line_file *f;
  168. {
  169.     struct g_line_vector *tmp_vec;
  170.  
  171.     f->gdb_line_file_number_of_vectors++;
  172.     tmp_vec=(struct g_line_vector *)xmalloc(sizeof(struct g_line_vector));
  173.     tmp_vec->gdb_line_number_of_lines=0;
  174.     tmp_vec->gdb_line_lines=(struct g_line *)xmalloc(MAX_LINES_PER_VECTOR*sizeof(struct g_line));
  175.     tmp_vec->gdb_line_next_vector=f->gdb_line_file_vectors;
  176.     f->gdb_line_file_vectors=tmp_vec;
  177.     return tmp_vec;
  178. }
  179.  
  180. /* All done.  Time to write the stuff out.  This should be fun. */
  181. void
  182. gdb_lines_emit()
  183. {
  184.     struct g_line_file *f;
  185.     struct g_line_vector *v,*old_v,*v_tmp;
  186.     struct g_line *current_line_pointer;    /* lp */
  187.     int    n;
  188.     int    previous_line_number;
  189.     long int current_gdb_segment_pos;
  190.     unsigned int number_of_things_in_table;
  191.  
  192.     for(f=first_file;f;f=f->gdb_line_file_next_file) {
  193.         if(!f->gdb_line_table_offset) {
  194.             as_warn("No .gdblinetab given for file %d.  Ignoring .gdbline(s) for it.");
  195.             continue;
  196.         }
  197.  
  198.         /* Reverse the linked list of vectors.  Since we built it
  199.            last entry first, this puts the first entry at the start
  200.            of the list.  Thus we can manage to put out low line #s
  201.            at the start of the table. . .*/
  202.         v_tmp=0;
  203.         old_v=0;
  204.         for(v=f->gdb_line_file_vectors;v;v=v_tmp) {
  205.             v_tmp=v->gdb_line_next_vector;
  206.             v->gdb_line_next_vector=old_v;
  207.             old_v=v;
  208.         }
  209.         f->gdb_line_file_vectors=old_v;
  210.  
  211.         /* Start putting stuff at the beginning of the table */
  212.         current_gdb_segment_pos=f->gdb_line_table_offset+sizeof(long int);
  213.         previous_line_number = -2;
  214.         number_of_things_in_table = 0;
  215.  
  216.         /* For every vector in the table: */
  217.         for(v=f->gdb_line_file_vectors;v;v=v->gdb_line_next_vector) {
  218.             current_line_pointer=v->gdb_line_lines;
  219.  
  220.             /* For every element of every vector */
  221.             for(n=v->gdb_line_number_of_lines;n;n--) {
  222.  
  223.                 if(current_line_pointer->gdb_line_line_number != previous_line_number + 1) {
  224.                     /* Write out the line number */
  225.                     gdb_alter(current_gdb_segment_pos, -(current_line_pointer->gdb_line_line_number));
  226.                     current_gdb_segment_pos+=sizeof(long int);
  227.                     number_of_things_in_table++;
  228.                 }
  229.                 previous_line_number = current_line_pointer->gdb_line_line_number;
  230.  
  231.                 /* And write out the address */
  232.                 gdb_alter(current_gdb_segment_pos,current_line_pointer->gdb_line_frag->fr_address+current_line_pointer->gdb_line_offset);
  233.                 current_gdb_segment_pos+=sizeof(long int);
  234.                 number_of_things_in_table++;
  235.  
  236.                 current_line_pointer++;
  237.             }
  238.         }
  239.         gdb_alter(f->gdb_line_table_offset,number_of_things_in_table);
  240.     }
  241. }
  242.